﻿#include "u_channel_panel.h"
#include "Window/Charts/u_quality_time_chart.h"

ChannelPanel::ChannelPanel(QString code, QWidget *parent) : MyWidget(parent)
{
    this->code = code;
    this->equ = globalData->GetEquByCode("001");

    this->timer = new QTimer;

    this->InitWidget();
    this->SetupConnections();
    this->Initialize();

    this->timer->start(150);


    setMouseTracking(true);
    this->setCursor(Qt::PointingHandCursor);
}

void ChannelPanel::InitWidget(){
    this->setStyleSheet("font-size:11pt;color:#fff");

    labInfo1 = new QLabel("设备1");
    labInfo2 = new QLabel("0"); labInfo2->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
    labInfo2->setStyleSheet("font-size:14pt; color:#49A761");

    labInfo3 = new QLabel("0"); labInfo3->setAlignment(Qt::AlignRight | Qt::AlignVCenter);
    labInfo3->setStyleSheet("font-size:14pt; color:#49A761");

    state1 = new TStateTool("温度");
    state1->SetValueHide();
    state1->SetTwinkle(200);

    state2 = new TStateTool("湿度");
    state2->SetValueHide();
    state2->SetTwinkle(200);

    tempBar = new QProgressBar();
    tempBar->setRange(0,100);
    tempBar->setTextVisible(false);
    tempBar->setValue(0);

    humBar = new QProgressBar();
    humBar->setRange(0,100);
    humBar->setTextVisible(false);
    humBar->setValue(0);
    humBar->setObjectName("orange");

    QGridLayout *infoLayout = new QGridLayout();
    infoLayout->setColumnStretch(1, 1);
    infoLayout->setColumnStretch(2, 10);
    infoLayout->setColumnStretch(4, 1);
    infoLayout->setMargin(15);
    infoLayout->setSpacing(15);
//    infoLayout->addWidget(new QLabel("设备：")  ,1 ,1, 1 ,1);
//    infoLayout->addWidget(labInfo1             ,1 ,2, 1 ,3);
    infoLayout->addWidget(state1               ,2 ,1, 1 ,1);
    infoLayout->addWidget(tempBar              ,2 ,2, 1 ,2);
    infoLayout->addWidget(labInfo2             ,2 ,4, 1 ,1);
    infoLayout->addWidget(state2               ,3 ,1, 1 ,1);
    infoLayout->addWidget(humBar               ,3 ,2, 1 ,2);
    infoLayout->addWidget(labInfo3             ,3 ,4, 1 ,1);

    QGroupBox *groupbox11 = new QGroupBox(equ->devname);
    groupbox11->setStyleSheet("QGroupBox{border: 1px solid #1978D9;}");
    groupbox11->setLayout(infoLayout);

    //---------------------------------------------------------

    QVBoxLayout *mainLayout = new QVBoxLayout();
    mainLayout->setMargin(0);
    mainLayout->addWidget(groupbox11);
    this->setLayout(mainLayout);
}

void ChannelPanel::SetupConnections(){
    connect(timer, SIGNAL(timeout()), this, SLOT(OnTimer()));
}

void ChannelPanel::Initialize(){

}

void ChannelPanel::OnTimer(){
//    if(!this->equ) return;
//    labInfo1->setText(equ->devname);
//    labInfo2->setText(QString("%1 ℃").arg(QString::number(equ->launch, 'f',1), 5));
//    labInfo3->setText(QString("%1 RH").arg(QString::number(equ->reflect, 'f',1), 5));

//    tempBar->setValue(equ->launch);
//    humBar->setValue(equ->reflect);
//    state1->SetState(!equ->GetTempAlarm());
//    state2->SetState(!equ->GetHumAlarm());
}

void ChannelPanel::mouseDoubleClickEvent(QMouseEvent *event){
    QualityTimeChart *chart = new QualityTimeChart(equ);

    TDialog *dialog = new TDialog();
    dialog->setTitle(QString("实时曲线[%1]").arg(equ->devname));
    dialog->addWidget(chart);
    dialog->resize(700,400);
    dialog->exec();
}

void ChannelPanel::paintEvent(QPaintEvent *){
//    //绘制边框
//    QPainter painter(this);
//    painter.setPen(QColor("#1978D9"));
//    painter.drawRect(0, 0,this->width()-1, this->height()-1);
}
